﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TractRecord
{
    public static partial class WebCommon
    {
        public enum MailMessageType
        {
            NewUser,
            AccountInformationUpdated,
            NewProject,
            ProjectNotAcceptingEmails,
            ProjectAcceptedEmail,
            InvalidProjectEmail,
            NewIncomingEmail
        }

        public static void GenerateEmailMessage(MailMessageType type, string Subject, string To, ListDictionary Replacements)
        {
            //Send email verfication email

            MailDefinition md = new MailDefinition();
            md.BodyFileName = "/MailSource/" + type.ToString() + ".txt";
            //md.From = ConfigurationManager.AppSettings["AdministratorEmail"];
            md.Subject = Subject;

            md.Priority = System.Net.Mail.MailPriority.High;
            
            Replacements.Add("<%ApplicationName%>", ConfigurationManager.AppSettings["ApplicationName"]);
            Replacements.Add("<%AdministratorEmail%>", ConfigurationManager.AppSettings["AdministratorEmail"]);

            System.Net.Mail.MailMessage msg;
            msg = md.CreateMailMessage(To, Replacements, new Control());

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

            smtp.Send(msg);
        }

        public static void GenerateEmailMessage(MailMessageType type, string Subject, ArrayList To, ListDictionary Replacements, List<Code.AttachedFile> files)
        {
            //Send email verfication email

            MailDefinition md = new MailDefinition();
            md.BodyFileName = "/MailSource/" + type.ToString() + ".txt";
            //md.From = ConfigurationManager.AppSettings["AdministratorEmail"];
            md.Subject = Subject;

            md.Priority = System.Net.Mail.MailPriority.High;

            Replacements.Add("<%ApplicationName%>", ConfigurationManager.AppSettings["ApplicationName"]);
            Replacements.Add("<%AdministratorEmail%>", ConfigurationManager.AppSettings["AdministratorEmail"]);

            System.Net.Mail.MailMessage msg;
            msg = md.CreateMailMessage(String.Join(",", (String[])To.ToArray(typeof(string))), Replacements, new Control());

            foreach (Code.AttachedFile file in files)
            {
                msg.Attachments.Add(new System.Net.Mail.Attachment(file.ms, file.contenttype));
            }

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

            smtp.Send(msg);

        }

        public static void GenerateEmailMessage(MailMessageType type, string Subject, string To, ListDictionary Replacements, List<Code.AttachedFile> files)
        {
            //Send email verfication email

            MailDefinition md = new MailDefinition();
            md.BodyFileName = "/MailSource/" + type.ToString() + ".txt";
            //md.From = ConfigurationManager.AppSettings["AdministratorEmail"];
            md.Subject = Subject;

            md.Priority = System.Net.Mail.MailPriority.High;

            Replacements.Add("<%ApplicationName%>", ConfigurationManager.AppSettings["ApplicationName"]);
            Replacements.Add("<%AdministratorEmail%>", ConfigurationManager.AppSettings["AdministratorEmail"]);

            System.Net.Mail.MailMessage msg;
            msg = md.CreateMailMessage(To, Replacements, new Control());

            foreach (Code.AttachedFile file in files)
            {
                msg.Attachments.Add(new System.Net.Mail.Attachment(file.ms, file.contenttype));
            }
            
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

            smtp.Send(msg);

        }

        public static void SendEmail(System.Net.Mail.MailMessage m)
        {
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

            smtp.Send(m);
        }

        public static string FormatDate(string dt, string format)
        {
            try
            {
                return Convert.ToDateTime(dt).ToString("G");
            }
            catch
            {
                return string.Empty;
            }
        }
        
        //public static object FormatDataForJQGrid(ArrayList data, int? numRows, int? page, int recordCount)
        //{

        //    int pageIndex = page ?? 1; //--- current page
        //    int pageSize = numRows ?? 10; //--- number of rows to show per page
        //    int totalRecords = recordCount; //--- number of total items from query
        //    int totalPages = (int)Math.Ceiling((decimal)totalRecords / (decimal)pageSize); //--- number of pages
            
        //    var value = new
        //    {
        //        totalpages = totalPages, //--- number of pages
        //        page = pageIndex, //--- current opage
        //        totalrecords = totalRecords, //--- total items
        //        rows = data
        //        ).ToArray()

        //    };

        //    return value;

        //}
    }
}